package com.pfc.files;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

import com.pfc.wifis.*;

/* kies
 * 
 * guarda lecturas con esta estructura:
 * nombre_lugar.txt
 * 
 * SSID_1 LEVEL_1 FREC_1 SSID_2 LEVEL_2 FREC_2 SSID_3 LEVEL_3 FREC_3 ...  (lectura 1)
 * SSID_1 LEVEL_1 FREC_1 SSID_2 LEVEL_2 FREC_2 SSID_3 LEVEL_3 FREC_3 ...  (lectura 2)
 * SSID_1 LEVEL_1 FREC_1 SSID_2 LEVEL_2 FREC_2 SSID_3 LEVEL_3 FREC_3 ...  (lectura 3)
 * ......
 */

public class FileManager {

	private String name;
	private String place;
	
	
	
	
	public FileManager(String directory, String nameFile) {
		super();
		this.name = ""+ directory +"/" + nameFile + ".txt";
		this.place = nameFile;
	}

	
	public ArrayList<Element> LineToElements(String linea){
		ArrayList<Element> lista = new ArrayList<Element>();
		String[] s = linea.split(" , ");
		for (int i=0 ; i<s.length; i++){
			lista.add(StringToElement(s[i]));
		}
		return lista;
	}
	
	
	public Element StringToElement(String linea){
		Element e = new Element();
		String[] s = linea.split(" ");
		e.setSsid(s[0]);
		e.setLevel(s[1]);
		e.setFrec(s[2]);
		e.setPlace(this.place);
		return e;
	}
	
	public String ElementsToString(ArrayList<Element> lectura){
		String s = "";
		for(int i=0; i<lectura.size(); i++){
			s = s + lectura.get(i).toString();
		}
		return s;
	}
	
	public ArrayList<Object> ReadFile(){
		  File archivo = null;
	      FileReader fr = null;
	      BufferedReader br = null;
	      ArrayList<Element> lectura = new ArrayList<Element>();
	      Element e = new Element();
	      ArrayList<Object> listaLecturas = new ArrayList<Object>();
	 
	      try {
	         archivo = new File (this.name);
	         fr = new FileReader (archivo);
	         br = new BufferedReader(fr);
	 
	         String linea;
	         while((linea = br.readLine())!=null){
	           lectura = LineToElements(linea);
	           listaLecturas.add(lectura);
	         }
	      }
	      catch(Exception ex){
	         ex.printStackTrace();
	      }finally{
	         try{                    
	            if( null != fr ){   
	               fr.close(); 
	               br.close();
	            }
	            return listaLecturas;
	         }catch (Exception e2){ 
	            e2.printStackTrace();
	            
	         }
	      }return listaLecturas;
	}

	
	public void WriteFile(ArrayList<Object> listaLecturas){
		FileWriter fichero = null;
        PrintWriter pw = null;
        try
        {
            fichero = new FileWriter(this.name);
            pw = new PrintWriter(fichero);
            for (int i=0 ; i<listaLecturas.size(); i++){
            	ArrayList<Element> lectura = (ArrayList<Element>)listaLecturas.get(i);
                pw.println(ElementsToString(lectura)+"\n");
            
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
	           if (null != fichero){
	              fichero.close();
	           	  pw.close();
	           	}
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }
	}

}
